package pers.xhh.shopapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.xhh.shopapi.common.Mi;
import pers.xhh.shopapi.common.ResultCode;
import pers.xhh.shopapi.dao.MiAttributeDao;
import pers.xhh.shopapi.entity.MiAttributeEntity;
import pers.xhh.shopapi.entity.to.attribute.MiAttributeTo;
import pers.xhh.shopapi.entity.vo.attribute.MiAttributeVo;
import pers.xhh.shopapi.myenum.AttrSelEnum;
import pers.xhh.shopapi.service.MiAttributeService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wei-xhh
 * @date 2020-11-22
 */
@Service
public class MiAttributeServiceImpl extends ServiceImpl<MiAttributeDao, MiAttributeEntity> implements MiAttributeService{

    @Override
    public Mi getAttrByCatIdAndSel(Integer id, String sel) {
        if(id == null || id <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }
        if(sel == null){
            return Mi.error(ResultCode.ERROR800.getMessage(), ResultCode.ERROR800.getStatus());
        }
        if(!sel.equals(AttrSelEnum.MANY.getAttrSel())
                && !sel.equals(AttrSelEnum.ONLY.getAttrSel())){
            return Mi.error(ResultCode.ERROR800.getMessage(), ResultCode.ERROR800.getStatus());
        }

        // ->查询
        QueryWrapper<MiAttributeEntity> queryWrapper =
                new QueryWrapper<MiAttributeEntity>().eq("cat_id", id).eq("attr_sel", sel);
        List<MiAttributeEntity> list = this.list(queryWrapper);
        if(list != null){
            List<MiAttributeVo> collect = list.stream().map(item -> {
                MiAttributeVo miAttributeVo = new MiAttributeVo();
                miAttributeVo.setAttr_id(item.getAttrId());
                miAttributeVo.setAttr_name(item.getAttrName());
                miAttributeVo.setAttr_sel(item.getAttrSel());
                miAttributeVo.setAttr_write(item.getAttrWrite());
                miAttributeVo.setAttr_vals(item.getAttrVals());
                miAttributeVo.setCat_id(item.getCatId());
                return miAttributeVo;
            }).collect(Collectors.toList());

            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), collect);
        }
        return Mi.error(ResultCode.ERROR500.getMessage(), ResultCode.ERROR500.getStatus());
    }

    @Override
    public Mi insertAttr(Integer id, MiAttributeTo miAttributeTo) {
        if(id == null || id <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }
        String attrName = miAttributeTo.getAttr_name();
        String attrSel = miAttributeTo.getAttr_sel();
        String attrVals = miAttributeTo.getAttr_vals();
        if(!attrSel.equals(AttrSelEnum.MANY.getAttrSel())
                && !attrSel.equals(AttrSelEnum.ONLY.getAttrSel())){
            return Mi.error(ResultCode.ERROR800.getMessage(), ResultCode.ERROR800.getStatus());
        }
        if(attrVals == null){
            attrVals = "";
        }
        MiAttributeEntity miAttributeEntity = new MiAttributeEntity();
        miAttributeEntity.setCatId(id);
        miAttributeEntity.setAttrName(attrName);
        miAttributeEntity.setAttrSel(attrSel);
        miAttributeEntity.setAttrVals(attrVals);
        miAttributeEntity.setCreateTime(new Date());
        miAttributeEntity.setUpdateTime(new Date());
        boolean save = this.save(miAttributeEntity);
        if(save){
            Map<String, Object> map = new HashMap<>();
            map.put("cat_id", id);
            map.put("attr_sel", attrSel);
            map.put("attr_name", attrName);
            map.put("attr_vals", attrVals);
            return Mi.ok(ResultCode.SUCCESS1.getMessage(), ResultCode.SUCCESS1.getStatus(), map);
        }
        return Mi.error(ResultCode.ERROR500.getMessage(), ResultCode.ERROR500.getStatus());
    }

    @Override
    public Mi deleteAttr(Integer catId, Integer attrId) {
        if(catId == null || catId <= 0 || attrId == null || attrId <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }
        // TODO 直接用attrId删除就可以了
        QueryWrapper<MiAttributeEntity> queryWrapper = new QueryWrapper<MiAttributeEntity>()
                .eq("attr_id", attrId)
                .eq("cat_id", catId);
        boolean remove = this.remove(queryWrapper);
        if(remove){
            Map<String, Object> map = new HashMap<>();
            map.put("attr_id", attrId);
            map.put("cat_id", catId);
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), map);
        }
        return Mi.error(ResultCode.ERROR500.getMessage(), ResultCode.ERROR500.getStatus());
    }

    @Override
    public Mi getAttr(Integer catId, Integer attrId) {
        if(catId == null || catId <= 0 || attrId == null || attrId <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }
        // TODO 直接用attrId查询就可以了
        MiAttributeEntity byId = this.getById(attrId);
        if(byId != null){
            MiAttributeVo miAttributeVo = new MiAttributeVo();
            miAttributeVo.setAttr_id(byId.getAttrId());
            miAttributeVo.setAttr_name(byId.getAttrName());
            miAttributeVo.setAttr_sel(byId.getAttrSel());
            miAttributeVo.setAttr_write(byId.getAttrWrite());
            miAttributeVo.setAttr_vals(byId.getAttrVals());
            miAttributeVo.setCat_id(byId.getCatId());

            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), miAttributeVo);
        }

        return Mi.error(ResultCode.ERROR500.getMessage(), ResultCode.ERROR500.getStatus());
    }

    @Override
    public Mi updateAttr(Integer catId, Integer attrId, MiAttributeTo miAttributeTo) {
        if(catId == null || catId <= 0 || attrId == null || attrId <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }
        String attrName = miAttributeTo.getAttr_name();
        String attrSel = miAttributeTo.getAttr_sel();
        String attrVals = miAttributeTo.getAttr_vals();
        if(!attrSel.equals(AttrSelEnum.MANY.getAttrSel())
                && !attrSel.equals(AttrSelEnum.ONLY.getAttrSel())){
            return Mi.error(ResultCode.ERROR800.getMessage(), ResultCode.ERROR800.getStatus());
        }
        MiAttributeEntity miAttributeEntity = new MiAttributeEntity();
        miAttributeEntity.setAttrId(attrId);
        miAttributeEntity.setCatId(catId);
        miAttributeEntity.setAttrName(attrName);
        miAttributeEntity.setAttrSel(attrSel);
        miAttributeEntity.setUpdateTime(new Date());
        if(attrVals != null){
            miAttributeEntity.setAttrVals(attrVals);
        }
        boolean save = this.updateById(miAttributeEntity);
        if(save){
            Map<String, Object> map = new HashMap<>();
            map.put("attr_id", attrId);
            map.put("cat_id", catId);
            map.put("attr_name", attrName);
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), map);
        }
        return Mi.error(ResultCode.ERROR500.getMessage(), ResultCode.ERROR500.getStatus());
    }
}
